package com.sy.common.persistence;

import com.sy.cloud.guess.model.Heroset;
import com.sy.common.model.Juejinuser;
import com.sy.common.persistence.BaseDao;
import com.sy.common.persistence.QueryHelper;
import com.sy.common.service.IBaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuwenquan on 2017-10-29.
 */

@Transactional
@Service
public class BaseService implements IBaseService {

    @Resource
    BaseDao baseDao;

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> T update(final T t) {
        return baseDao.update(t);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> T create(final T t) {
        return baseDao.create(t);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> void batchCreate(final List<T> list) {
        for(T t : list) {
            baseDao.create(t);
        }
        baseDao.flush();
        baseDao.clear();
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> void remove(T model) {
        baseDao.remove(model);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> T getOneByFieldValue(Class<T> modelClass, String fieldName, Object value) {
        return baseDao.getOneByFieldValue(modelClass, fieldName, value);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> T getById(Class<T> modelClass, final Object id) {
        return baseDao.getById(modelClass, id);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public Integer executeUpdate(final String hql, Object... params) {
        return baseDao.executeUpdate(hql, params);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> List<T> listQuery(final String hql, Class<T> modelClass) {
        return baseDao.listQuery(hql, modelClass);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> List<T> listQueryFromMaster(final String hql, Class<T> modelClass) {
        return baseDao.listQuery(hql, modelClass);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> List<T> listQuery(final String hql, Class<T> modelClass, Object... params) {
        return baseDao.listQuery(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> List<T> listQueryFromMaster(final String hql, Class<T> modelClass, Object... params) {
        return baseDao.listQuery(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> List<T> listQuery(final String hql, Class<T> modelClass, List<Object> params) {
        return baseDao.listQuery(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> List<T> listQueryForIn(final String hql, Class<T> modelClass, List<Object> params) {
        return baseDao.getAllByHQL(hql, modelClass, params);
    }
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> List<T> listQueryFromMaster(final String hql, Class<T> modelClass, List<Object> params) {
        return baseDao.listQuery(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> T singleQuery(final String hql, Class<T> modelClass) {
        return baseDao.singleQuery(hql, modelClass);
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> T singleQuery(final String hql, Class<T> modelClass, Object... params) {
        return baseDao.singleQuery(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> T singleQueryFromMaster(final String hql, Class<T> modelClass) {
        return baseDao.singleQuery(hql, modelClass);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> T singleQueryFromMaster(final String hql, Class<T> modelClass, Object... params) {
        return baseDao.singleQuery(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void flush() {
        baseDao.flush();
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> List<T> listQueryForPage(final String hql, Class<T> modelClass, QueryHelper queryHelper) {
        return baseDao.listQueryForPage(hql, modelClass, queryHelper);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> List<T> listQueryForPageFromMaster(final String hql, Class<T> modelClass, QueryHelper queryHelper) {
        return baseDao.listQueryForPage(hql, modelClass, queryHelper);
    }

    @Override
    @Transactional(propagation= Propagation.NOT_SUPPORTED, readOnly = true)
    public List<Object[]> nativeQuery(final String sql) {
        return baseDao.nativeQuery(sql);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public List<Object[]> nativeQueryFromMaster(final String sql) {
        return baseDao.nativeQuery(sql);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void nativeUpdate(final String sql) {
        baseDao.nativeUpdate(sql);
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void nativeUpdate(final String sql, Object... params) {
        baseDao.nativeUpdate(sql, params);
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public void nativeUpdate(final String sql, Map<String, Object> paramMap) {
        baseDao.nativeUpdate(sql, paramMap);
    }

    @Override
    @Transactional(propagation= Propagation.NOT_SUPPORTED, readOnly = true)
    public List<Object> nativeQuerySingleColumn(final String sql) {
        return baseDao.nativeQuerySingleColumn(sql);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public List<Object> nativeQuerySingleColumnFromMaster(final String sql) {
        return baseDao.nativeQuerySingleColumn(sql);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int prc_call(final String sql)
    {
        return baseDao.prc_call(sql);

    }

    @Override
    @Transactional(propagation= Propagation.NOT_SUPPORTED, readOnly = true)
    public int nativeQueryGetCount(String sql) {
        List<Object> list = nativeQuerySingleColumn(sql);
        BigInteger cnt = (BigInteger) list.get(0);
        return cnt.intValue();
    }
    @Override
    @Transactional(propagation= Propagation.NOT_SUPPORTED, readOnly = true)
    public int nativeQueryGetInt(String sql) {
        List<Object> list = nativeQuerySingleColumn(sql);
        Number cnt = (Number) list.get(0);
        return cnt == null ? -1 : cnt.intValue();
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int nativeQueryGetCountFromMaster(String sql) {
        List<Object> list = nativeQuerySingleColumn(sql);
        BigInteger cnt = (BigInteger) list.get(0);
        return cnt.intValue();
    }

    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> int getCountByFieldValue(Class<T> modelClass, String fieldName, Object value) {
        return baseDao.getCountByFieldValue(modelClass, fieldName, value);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> int getCountByFieldValueFromMaster(Class<T> modelClass, String fieldName, Object value) {
        return baseDao.getCountByFieldValue(modelClass, fieldName, value);
    }

    @Override
    public <T> T findByPhone(String phone) {
        return null;
    }



    @Override
    @Transactional(propagation= Propagation.SUPPORTS, readOnly = true)
    public <T> T queryObject(final String hql, Class<T> modelClass, Object... params) {
        return baseDao.queryObject(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> T queryObjectFromMaster(final String hql, Class<T> modelClass, Object... params) {
        return baseDao.queryObject(hql, modelClass, params);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public <T> T getByIdFromMaster(Class<T> modelClass, Object id) {
        return baseDao.getById(modelClass, id);
    }

    public boolean ifPhoneExist(String phone){
        StringBuffer hql = new StringBuffer();
        hql.append("From Juejinuser where phone='");
        hql.append( phone );
        hql.append("'");
        List<Juejinuser> juejinuserList = baseDao.listQuery(hql.toString(), Juejinuser.class);
        if( juejinuserList.size() > 0 ){
            return false;
        }else{
            return true;
        }
    }
}
